package com.hpq.common.event.listener;

import com.hpq.common.domain.enums.UserActiveStatusEnum;
import com.hpq.common.event.UserBlackEvent;
import com.hpq.common.event.UserOnlineEvent;
import com.hpq.user.dao.UserDAO;
import com.hpq.user.domain.model.User;
import com.hpq.user.service.IpService;
import com.hpq.user.service.UserBackpackService;
import com.hpq.websocket.domain.vo.request.WSBaseReq;
import com.hpq.websocket.service.WebSocketService;
import com.hpq.websocket.service.adapter.WebSocketAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 *  用户注册监听器
 */
@Component
@Slf4j
public class UserBlackListener {
    @Autowired
    private UserDAO userDao;
    @Autowired
    private UserBackpackService userBackpackService;
    @Autowired
    private IpService ipService;
    @Autowired
    private WebSocketService webSocketService;

    /**
     *  发送黑名单消息
     * @param event
     */
    @Async
    @TransactionalEventListener(value = UserBlackEvent.class,phase = TransactionPhase.AFTER_COMMIT,fallbackExecution = true)
    public void sendMsg(UserBlackEvent event){
        User user = event.getUser();
        webSocketService.sendMsgToAll(WebSocketAdapter.buildBlackResp(user));

    }

    /**
     *  改变用户状态
     * @param event
     */
    @Async
    @TransactionalEventListener(value = UserBlackEvent.class,phase = TransactionPhase.AFTER_COMMIT,fallbackExecution = true)
    public void changeUserStatus(UserBlackEvent event){
        userDao.invalidUid(event.getUser().getId());
    }

}
